package com.metaera.app.ui.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.metaera.app.R;
import com.metaera.app.controller.presenter.UserEditPresenter;
import com.metaera.app.controller.view.MsgNetworkRequestView;
import com.metaera.app.data.model.UserDetailsResponse;
import com.metaera.app.ui.core.BaseActivity;
import com.metaera.app.utils.ActivityTitleHelper;
import com.metaera.app.utils.ToastUtils;
import com.metaera.app.utils.ViewUtils;

public class UserEditItemActivity extends BaseActivity {

    public static final int
            NICKNAME_TYPE         = 1,
            TITLE_TYPE            = 2,
            INTRODUCE_TYPE        = 3,
            WEIBO_TYPE            = 4,
            KNOWLEDGE_TYPE        = 5,
            OFFICIAL_ACCOUNT_TYPE = 6,
            TWITTER_TYPE          = 7,
            EMAIL_TYPE            = 8,
            PHONE_TYPE            = 9,
            SEX_TYPE            = 10,
            WECHAT_TYPE           = 11;


    public static Intent newInstance(
            Context context, int type, CharSequence content, int isShowWechat, int isShowPhone, int sex) {
        Intent intent = new Intent(context, UserEditItemActivity.class);
        intent.putExtra("TYPE", type);
        intent.putExtra("SEX", sex);
        intent.putExtra("IS_SHOW_PHONE", isShowPhone);
        intent.putExtra("IS_SHOW_WE_CHAT", isShowWechat);
        if (content != null) {
            intent.putExtra("CONTENT", content.toString());
        }
        return intent;
    }


    private UserEditPresenter presenter;


    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_user_edit_item;
    }

    @Override
    protected void initialize(Bundle savedInstanceState) {
        CheckBox checkBox = findViewById(R.id.check_box);
        TextView tvAction = findViewById(R.id.tv_action);
        EditText editContent = findViewById(R.id.edit_content);
        tvAction.setText(R.string.save);
        tvAction.setTextColor(getColorForRes(R.color.colorPrimary));

        Intent intent = getIntent();
        int type = intent.getIntExtra("TYPE", 0);
        int sex = intent.getIntExtra("SEX", 0);
        int isShowPhone = intent.getIntExtra("IS_SHOW_PHONE", 0);
        int isShowWechat = intent.getIntExtra("IS_SHOW_WE_CHAT", 0);
        String content = intent.getStringExtra("CONTENT");

        if (content != null) {
            editContent.setText(content);
        }

        presenter = new UserEditPresenter();
        presenter.setModelAndView(new MsgNetworkRequestView());

        UserDetailsResponse user = new UserDetailsResponse();
        user.setSex(sex);
        user.setIs_show_wechat(isShowWechat);
        user.setIs_show_phone(isShowPhone);

        switch (type) {
            case NICKNAME_TYPE:
                ActivityTitleHelper.setTitle(this, String.format(getString(R.string.u_edit_placeholder), getString(R.string.user_edit_nickname)));

                tvAction.setOnClickListener(v -> {
                    if (TextUtils.isEmpty(editContent.getText())){
                        ToastUtils.makeText("不能为空");
                        return;
                    }

//                    if(editContent.getText().length() > 12){
//                        ToastUtils.makeText("昵称长度不能超过12个字符");
//                        return;
//                    }

                    user.setNickname(editContent.getText().toString());
                    presenter.update(user);
                });
                break;

            case SEX_TYPE:
                ActivityTitleHelper.setTitle(this, String.format(getString(R.string.u_edit_placeholder), getString(R.string.user_edit_sex)));
                TextView tvMan = findViewById(R.id.tv_man);
                TextView tvWoman = findViewById(R.id.tv_woman);
                tvAction.setText(null);
                editContent.setVisibility(View.GONE);
                ViewUtils.setVisibility(View.VISIBLE, tvMan, tvWoman);

                presenter.setModelAndView(new MsgNetworkRequestView(){

                    @Override
                    public void successful(String data) {
                        super.successful(data);
                        finish();
                    }

                });

                tvMan.setOnClickListener(v -> {
                    user.setSex(1);
                    presenter.update(user);
                });

                tvWoman.setOnClickListener(v -> {
                    user.setSex(0);
                    presenter.update(user);
                });
                break;

            case TITLE_TYPE:
                ActivityTitleHelper.setTitle(this, String.format(getString(R.string.u_edit_placeholder), getString(R.string.user_edit_title)));
                editContent.setHint(R.string.user_edit_title_desc);

                tvAction.setOnClickListener(v -> {
                    user.setPosition(editContent.getText().toString());
                    presenter.update(user);
                });
                break;

            case INTRODUCE_TYPE:
                editContent.setMinLines(4);
                editContent.setImeOptions(EditorInfo.IME_NULL);
                editContent.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);

                ActivityTitleHelper.setTitle(this, String.format(getString(R.string.u_edit_placeholder), getString(R.string.user_edit_introduce)));
                editContent.setHint(R.string.user_edit_introduce_desc);

                tvAction.setOnClickListener(v -> {
                    user.setDesc(editContent.getText().toString());
                    presenter.update(user);
                });
                break;

            case WEIBO_TYPE:
                ActivityTitleHelper.setTitle(this, String.format(getString(R.string.u_edit_placeholder), getString(R.string.user_edit_weibo)));
                editContent.setHint(R.string.user_edit_weibo_desc);

                tvAction.setOnClickListener(v -> {
                    user.setWeibo(editContent.getText().toString());
                    presenter.update(user);
                });
                break;

            case KNOWLEDGE_TYPE:
                ActivityTitleHelper.setTitle(this, String.format(getString(R.string.u_edit_placeholder), getString(R.string.user_edit_knowledge_star)));
                editContent.setHint(R.string.user_edit_knowledge_star_desc);

                tvAction.setOnClickListener(v -> {
                    user.setKnowledge_planet(editContent.getText().toString());
                    presenter.update(user);
                });
                break;

            case OFFICIAL_ACCOUNT_TYPE:
                ActivityTitleHelper.setTitle(this, String.format(getString(R.string.u_edit_placeholder), getString(R.string.user_edit_official_accounts)));
                editContent.setHint(R.string.user_edit_official_accounts_desc);

                tvAction.setOnClickListener(v -> {
                    user.setOfficial_account(editContent.getText().toString());
                    presenter.update(user);
                });
                break;

            case TWITTER_TYPE:
                ActivityTitleHelper.setTitle(this, String.format(getString(R.string.u_edit_placeholder), getString(R.string.user_edit_twitter)));
                editContent.setHint(R.string.user_edit_twitter_desc);

                tvAction.setOnClickListener(v -> {
                    user.setTwitter(editContent.getText().toString());
                    presenter.update(user);
                });
                break;

            case EMAIL_TYPE:
                ActivityTitleHelper.setTitle(this, String.format(getString(R.string.u_edit_placeholder), getString(R.string.user_edit_email)));
                editContent.setHint(R.string.user_edit_email_desc);

                tvAction.setOnClickListener(v -> {
                    user.setEmail(editContent.getText().toString());
                    presenter.update(user);
                });
                break;

            case PHONE_TYPE:
                checkBox.setVisibility(View.VISIBLE);
                checkBox.setChecked(isShowPhone == 1);
                user.setPhone(editContent.getText().toString());

                checkBox.setText(R.string.user_edit_phone_tips);
                editContent.setInputType(InputType.TYPE_CLASS_PHONE);

                ActivityTitleHelper.setTitle(this, String.format(getString(R.string.u_edit_placeholder), getString(R.string.user_edit_phone_number)));
                editContent.setHint(R.string.user_edit_phone_number_desc);

                tvAction.setOnClickListener(v -> {
                    user.setIs_show_phone(checkBox.isChecked() ? 1 : 0);
                    user.setPhone(editContent.getText().toString());
                    presenter.update(user);
                });
                break;

            case WECHAT_TYPE:
                checkBox.setVisibility(View.VISIBLE);
                checkBox.setChecked(isShowWechat == 1);
                user.setWechat(editContent.getText().toString());

                checkBox.setText(R.string.user_edit_wx_tips);
                editContent.setInputType(InputType.TYPE_CLASS_PHONE);

                ActivityTitleHelper.setTitle(this, String.format(getString(R.string.u_edit_placeholder), getString(R.string.user_edit_wechat)));
                editContent.setHint(R.string.user_edit_wechat_desc);

                tvAction.setOnClickListener(v -> {
                    user.setIs_show_wechat(checkBox.isChecked() ? 1 : 0);
                    user.setWechat(editContent.getText().toString());
                    presenter.update(user);
                });
                break;
        }
    }

}
